// pages/goodsDetails/goodsDetails.js
const app = getApp()
Page({
  data: {
    xspr: '',
    hasadd: false,
    spnum: 1,
    num: "4455",
    hasadd: false,
    goods_imgs: [
      { url: '../../img/a_51_03.png' },
      { url: '../../img/a_51_03.png' },
      { url: '../../img/a_51_03.png' }
    ],
    goods_name: '',
    goods_des: '',
    goods_img: '',
    goods_price: '',
    gid: 1,
    goods_spec: [],
    address:'',
    infor: [
      {

      }
    ],
    aaa: { name: '规格1', specid: '', stock: 0, max_price: '1000' },
    alltime: [
      { name: '规格1', stock: 0, specid: '51', max_price: '1000' },
      { name: '规格1', stock: 0, specid: '53', max_price: '2000' },
      { name: '规格1', stock: 0, specid: '25', max_price: '3000' },
      { name: '规格1', stock: 0, specid: '54', max_price: '4000' },

    ],
    configure: [
      { type: "车载类型", value: "车载充电器" },
      { type: "插口", value: "双口" },
      { type: "适用机型", value: "苹果手机，三星手机，华为手机，小米手机" },
      { type: "风格", value: "商务" },
      { type: "原装配件", value: "小米原装" },
      { type: "售后服务", value: "全国联保" },
      { type: "产地", value: "中国" },
      { type: "备注", value: "官方标配" },
      { type: "编号", value: "XP-11122333" }
    ],
    types: null,
    bid :0,
  },
  kshover: function (e) {
    let that = this;
    var list = that.data.alltime;
    var id   = e.currentTarget.dataset.index;
    that.setData({
      id: id,
      aaa: that.data.alltime[id]
    })
  },
  addnum: function () {
    var spnum = this.data.spnum;
    spnum++;
    this.setData({
      spnum: spnum
    })
  },
  cannum: function () {
    var spnum = this.data.spnum;
    spnum--;
    if (spnum <= 0) {
      spnum = 1;
    }
    this.setData({
      spnum: spnum
    })
  },
  //加入购物车
  showjrinfo: function (options) {
    var that = this;
    var uid = wx.getStorageSync('uid');
    var gid = options.currentTarget.id;
    var types = that.data;
    wx.request({
      url    : app.url + '/Goods/goodsSpec',
      method : 'post',
      data   : { uid: uid, gid: gid, types: types },
      success: function (res) {
        that.setData({ alltime: res.data.data })
      },
      error: function (res) {}
    })
    this.setData({
      isshow: true
    })
  },
  showjrshop: function (options) {
    var that  = this;
    var uid   = wx.getStorageSync('uid');
    var gid   = options.currentTarget.id;
    var types = that.data;
    wx.request({
      url: app.url + '/Goods/goodsSpec',
      method: 'post',
      data: { uid: uid, gid: gid, types: types },
      success: function (res) {
        that.setData({ alltime: res.data.data })
      },
      error: function (res) { }
    })
    this.setData({
      isshop: true
    })
  },
  tocloase: function () {
    this.setData({
      isshow: false
    })
  },
  toloasesjo: function () {
    this.setData({
      isshop: false
    })
  },
  // 加入购物车
  add_car: function (options) {
    var that   = this;
    var uid    = wx.getStorageSync('uid');
    var gid    = options.currentTarget.id;
    var specid = that.data.aaa.specid;
    if (specid==null || specid=='' || specid==undefined || specid==false){
      return false;
    }
    wx.request({
      url     : app.url + '/Goods/cart_add',
      method  : 'post',
      data    : {
        uid   : uid,
        specid: that.data.aaa.specid,
        num   : that.data.spnum,
        bid   : that.data.bid
      },
      success : function (res) {
        if (res.data.code == 1) {
          wx.showToast({ title: '添加成功', duration: 1500 })
        } else {
          wx.showToast({ title: '添加失败', duration: 1500 })
        }
      },
      erroe: function (res) { }
    })
  },
  // 点进进入购物车
  tocar: function () {
    wx.navigateTo({ url: '../car/car' })
  },
  add_buy: function (e) {
    var that = this;
    var type = that.data.types;
    var spec = that.data.aaa.specid;
    console.log(that.data.address)
    if (!spec){
      wx.showToast({
        title: '请选择规格',
      })
      return false;
    } 
    if (!that.data.address) {
      wx.showModal({
        title: '地址信息',
        content: '地址为空，清先添加地址',
        success:function(res){
          if (res.confirm) {
            wx.navigateTo({
              url: '../myadsxz/myadsxz'
            })
          }else if (res.cancel){
            console.log('用户点击取消')
          }
        }
      })
      return false;
    }
    var quantity = that.data.spnum;
    var bid = that.data.bid;
    wx.navigateTo({ url: '../byorder/byorder?num=' + spec + '&quantity=' + quantity+'&bid='+bid+
    '&type='+type})
  },
  // 顶部
  goTop: function (e) {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0,
        duration: 300
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var gid  = options.gid;
    var type = options.types;
    if (type == 1){
      var bid = options.bid;
      that.setData({ bid: bid })
    }else{
      that.setData({bid : 0})
    }
    var uid  = wx.getStorageSync('uid');
    that.setData({
      gid: gid,
      types: options.types
    })
    wx.request({
      url: app.url + '/Goods/goodsDetail',
      method: 'post',
      data: { gid: gid, uid: uid,bid:bid },
      success: function (resDetails) {
        console.log('resDetails',resDetails)
        if (resDetails.data.code == 1) {
          var newGoodsDetails = [];
          var newGoodsDeatilsImgs = [];
          var temporaryImgs = resDetails.data.data['pics'].split(',');
          for (var i = 0; i < temporaryImgs.length; i++) {
            newGoodsDeatilsImgs.push({
              url: app.uploadUrl + temporaryImgs[i]
            })
          }
          newGoodsDetails.push({
            type: '适用机型',
            value: resDetails.data.data.suitable_model
          });
          newGoodsDetails.push({
            type: '服务热线',
            value: resDetails.data.data.phone + '   (直接拨打)'
          });
          newGoodsDetails.push({
            type: '免责提示：',
            value: '咨询完客服，私下交易，损失自付'
          });
          // newGoodsDetails.push({
          //   type: '车载类型',
          //   value: resDetails.data.data.vehicle_type
          // });
          // newGoodsDetails.push({
          //   type: '插口',
          //   value: resDetails.data.data.jack
          // });
          // newGoodsDetails.push({
          //   type: '风格',
          //   value: resDetails.data.data.style
          // });
          // newGoodsDetails.push({
          //   type: '原装配件',
          //   value: resDetails.data.data.original_accessories
          // });
          // newGoodsDetails.push({
          //   type: '产地',
          //   value: resDetails.data.data.orign
          // });
          // newGoodsDetails.push({
          //   type: '备注',
          //   value: resDetails.data.data.remark
          // });
          // newGoodsDetails.push({
          //   type: '编号',
          //   value: resDetails.data.data.number
          // });
          that.setData({
            configure  : newGoodsDetails,
            goods_imgs : newGoodsDeatilsImgs,
            goods_name : resDetails.data.data.name,
            goods_des  : resDetails.data.data.des,
            goods_img  : app.uploadUrl + resDetails.data.data.pic,
            goods_price: resDetails.data.data.price,
            volume: resDetails.data.data.volume,
          })
        }
      }
    })
    wx.request({
      url: app.url + '/user/address',
      data: { uid: wx.getStorageSync('uid') },
      method: 'GET',
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          res.data.data = '';
        }
        that.setData({
          address: res.data.data
        })
      },
    })
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})